package com.mkj.draw;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @Author Arrebol
 * @Date 2024/8/5 16:13
 * @Project graphical programming
 * @Description： 五子棋游戏
 */
public class GoBang {

    //定义五子棋游戏窗口
    //private final Frame frame = new Frame("五子棋");
    private final JFrame frame = new JFrame("五子棋");
    //声明四个BufferedImage对象分别记录四张图片
    BufferedImage table;
    BufferedImage blackChess;
    BufferedImage whiteChess;
    BufferedImage selectBoard;
    //声明棋盘的宽度与高度
    private final int TABLE_WIDTH = 535;
    private final int TABLE_HEIGHT = 536;
    //声明棋盘横向与纵向分别可以下多少颗棋子
    private final int BOARD_SIZE = 15;
    //声明每个棋子占用棋盘的比率
    private final int RATE = TABLE_WIDTH / BOARD_SIZE;
    //声明记录棋子对于X方向和Y方向的偏移量
    private final int X_OFFSET = 5;
    private final int Y_OFFSET = 6;
    //声明二维数组记录棋子，0——没有棋子，1——白棋，2——黑棋
    int[][] board = new int[BOARD_SIZE][BOARD_SIZE];
    //声明红色选择框的坐标
    int selectX = -1;
    int selectY = -1;

    //自定义继承Canvas的类
    private class chessBoard extends JPanel {
        @Override
        public void paint(Graphics g) {
            //绘图
            //绘制棋盘
            g.drawImage(table, 0, 0, null);
            //绘制选择框
            if (selectX > 0 && selectY > 0) {
                g.drawImage(selectBoard, selectX*RATE + X_OFFSET, selectY*RATE + Y_OFFSET, null);
            }
            //绘制棋子
            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] == 2) {
                        //是黑棋，绘制黑棋
                        g.drawImage(blackChess, i * RATE + X_OFFSET, j * RATE + Y_OFFSET, null);
                    } else if (board[i][j] == 1) {
                        //是白棋，绘制白棋
                        g.drawImage(whiteChess, i * RATE + X_OFFSET, j * RATE + Y_OFFSET, null);
                    }
                }
            }
        }
    }

    chessBoard chessBoard = new chessBoard();
    //声明记录当前执棋的颜色
    private int boardType = 2;
    //声明底部需要的组件
    Panel panel = new Panel();
    Button whiteButton = new Button("白棋");
    Button blackButton = new Button("黑棋");
    Button deleteButton = new Button("删除");

    public void refreshButtonColor(Color whiteButtonColor,Color blackButtonColor,Color deleteButtonColor){
        whiteButton.setBackground(whiteButtonColor);
        blackButton.setBackground(blackButtonColor);
        deleteButton.setBackground(deleteButtonColor);
    }
    public void init() throws IOException {
        //组装视图，编写逻辑
        whiteButton.addActionListener(e->{
            //修改执棋颜色
            boardType = 1;
            //刷新按钮的颜色
            refreshButtonColor(Color.GREEN,Color.GRAY,Color.GRAY);
        });
        blackButton.addActionListener(e->{
            //修改执棋颜色
            boardType = 2;
            //刷新按钮的颜色
            refreshButtonColor(Color.GRAY,Color.GREEN,Color.GRAY);
        });
        deleteButton.addActionListener(e -> {
            //修改执棋颜色
            boardType = 0;
            //刷新按钮的颜色
            refreshButtonColor(Color.GRAY,Color.GRAY,Color.GREEN);
        });
        panel.add(blackButton);
        panel.add(whiteButton);
        panel.add(deleteButton);
        frame.add(panel,BorderLayout.SOUTH);
        //初始化图片
        table = ImageIO.read(new File("awt\\image\\board.jpg"));
        blackChess = ImageIO.read(new File("awt\\image\\black.gif"));
        whiteChess = ImageIO.read(new File("awt\\image\\white.gif"));
        selectBoard = ImageIO.read(new File("awt\\image\\selected.gif"));
        //设置棋盘
        //处理鼠标移动
        chessBoard.addMouseMotionListener(new MouseAdapter() {
            //当鼠标移动时会调用方法
            @Override
            public void mouseMoved(MouseEvent e) {
                selectX = (e.getX()-X_OFFSET)/RATE;
                selectY = (e.getY()-Y_OFFSET)/RATE;
                chessBoard.repaint();
            }
        });
        //处理鼠标点击
        chessBoard.addMouseListener(new MouseAdapter() {
            //鼠标点击后会调用此方法
            @Override
            public void mouseClicked(MouseEvent e) {
                int posX = (e.getX() - X_OFFSET) / RATE;
                int posY = (e.getY() -Y_OFFSET) / RATE;
                board[posX][posY] = boardType;
                chessBoard.repaint();
            }

            //鼠标从画面中出去时会调用方法
            @Override
            public void mouseExited(MouseEvent e) {
                selectX = -1;
                selectY = -1;
            }
        });
        chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
        frame.add(chessBoard);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //设置Frame最佳大小并设置其可见
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args){
        try {
            new GoBang().init();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
